package com.shop.shoppingmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShShopCart;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShShopCartService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 购物车(ShShopCart)表控制层
 *
 * @author makejava
 * @since 2020-09-09 11:39:54
 */
@RestController
@RequestMapping("shShopCart")
@CrossOrigin
public class ShShopCartController {
    /**
     * 服务对象
     */
    @Resource
    private ShShopCartService shShopCartService;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShShopCart selectOne(String id) {
        return this.shShopCartService.queryById(id);
    }

    /**
     * 添加购物车
     * @param jsonObject
     * @return
     */
    @PostMapping("insertShopCart")
    public Result insertShopCart(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        ShShopCart shShopCart = JSON.parseObject(jsonObject.toJSONString(), ShShopCart.class);
        String name1 = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name1);
        if(shUser == null || name1 == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        shShopCart.setUserId(shUser.getId());
        return shShopCartService.insert(shShopCart);
    }

    /**
     * 修改购物车商品数量
     * @param jsonObject
     * @return
     */
    @PostMapping("updateShopCart")
    public Result updateShopCart(@RequestBody JSONObject jsonObject) {
        ShShopCart shShopCart = JSON.parseObject(jsonObject.toJSONString(), ShShopCart.class);
        return shShopCartService.update(shShopCart);
    }

    /**
     * 删除购物车商品
     * @param cartId
     * @return
     */
    @GetMapping("deleteShopCart")
    public Result deleteShopCart(@RequestParam("cartId") String cartId) {
        return shShopCartService.deleteById(cartId);
    }

    /**
     * 根据用户id查询购物车全部数据
     * @param request
     * @return
     */
    @GetMapping("queryUserId")
    public Result queryUserId(HttpServletRequest request) {
        String name1 = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name1);
        if(shUser == null || name1 == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        return shShopCartService.queryUserId(shUser.getId());
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @GetMapping("deleteBatchByParams")
    public Result deleteBatchByParams(@RequestParam("ids") List<String> ids)
    {
        System.out.println(ids);
        return shShopCartService.deleteBatchByParams(ids);
    }

}